package vn.nt.healthcare.server.ws.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.nt.healthcare.server.ws.domain.Company;
import vn.nt.healthcare.server.ws.model.Customer;
import vn.nt.healthcare.server.ws.model.Feedback;
import vn.nt.healthcare.server.ws.repositories.CompanyRepository;
import vn.nt.healthcare.server.ws.service.FeedbackService;

@Service("feedbackService")
@Transactional
public class FeedbackServiceImpl implements FeedbackService {

	@Autowired
	private CompanyRepository companyRepository;

	@Override
	public boolean save(String companyId, int customerId, String comment,
			int goal) {
		Company company = companyRepository.findByCode(companyId);
		if (company != null) {
			Session session = null;
			Transaction transaction = null;
			try {
				session = ServiceHelper.openSession(company);
				transaction = session.beginTransaction();
				Feedback fb = new Feedback();
				fb.setComment(comment);
				fb.setGoal(goal);
				Customer customer = (Customer) session.get(Customer.class,
						customerId);
				fb.setCustomer(customer);
				fb.setCreatedDate(new Date());
				session.saveOrUpdate(fb);
				transaction.commit();
				return true;
			} catch (Exception e) {
				// TODO: handle exception
				if (transaction != null) {
					transaction.rollback();
				}
				e.printStackTrace();
			} finally {
				ServiceHelper.closeSession(session);
			}
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Feedback> findAllFeedbacks(String companyId) {
		// TODO Auto-generated method stub
		Company company = companyRepository.findByCode(companyId);
		if (company != null) {
			Session session = null;
			try {
				session = ServiceHelper.openSession(company);
				StringBuilder sql = new StringBuilder(
						"SELECT DISTINCT fb FROM Feedback fb WHERE 1 = 1 ORDER BY fb.createdDate desc");
				Query query = session.createQuery(sql.toString());
				return query.list();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ServiceHelper.closeSession(session);
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Feedback> findFeedbacksInTime(String companyId, Date fromDate,
			Date toDate) {
		Company company = companyRepository.findByCode(companyId);
		if (company != null) {
			Session session = null;
			try {
				session = ServiceHelper.openSession(company);
				StringBuilder sql = new StringBuilder(
						"SELECT DISTINCT fb FROM Feedback fb WHERE 1 = 1");
				sql.append(" AND fb.createdDate >= :fromDate");
				sql.append(" AND fb.createdDate <= :toDate");
				sql.append(" ORDER BY fb.createdDate desc");
				Query query = session.createQuery(sql.toString());
				query.setTimestamp("fromDate", fromDate);
				query.setTimestamp("toDate", toDate);
				return query.list();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ServiceHelper.closeSession(session);
			}
		}
		return null;
	}

}
